/*
 * Created on 2005-3-11
 */
package com.hd123.rumba.domain;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.hibernate.HibernateException;

import com.hd123.rumba.cartridge.ICartFunctionView;
import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.exception.StringTooLongException;
import com.hd123.rumba.runtime.NamingMgr;
import com.hd123.rumba.runtime.VersionedPersistent;
import com.hd123.rumba.user.IProfile;
import com.hd123.rumba.user.Profile;

/**
 * @author caili
 * @author lxm
 */
public class Domain extends VersionedPersistent implements IDomain {

  // 不从BaseEntity继承. 如果继承, web service公布类型时, 会先公布Domain, 再BaseEntity,
  // 使得delphi文件中类定义次序颠倒而不能编译.
  private IDomain domain;

  private String lastModifier;
  /** @deprecated */
  public static final int LENGTH_LASTMODIFIER = CDomain.LENGTH_LASTMODIFIER;

  private Integer state;

  private String code; // 代码
  /** @deprecated */
  public static final int LENGTH_CODE = CDomain.LENGTH_CODE;

  private String name; // 名称
  /** @deprecated */
  public static final int LENGTH_NAME = CDomain.LENGTH_NAME;

  private String remark; // 备注
  /** @deprecated */
  public static final int LENGTH_REMARK = CDomain.LENGTH_REMARK;

  private IProfile profile = new Profile(); // 档案

  private IProfile profile2 = new Profile();

  private IDomain settleOrganization = null; // 结算单位

  private String taxCode; // 税号
  public static final int LENGTH_TAXCODE = 40;

  private String invoiceCode; // 发票代码
  public static final int LENGTH_INVOICECODE = 40;

  private String bankAccount; // 银行帐号
  public static final int LENGTH_BANKACCOUNT = 40;

  private Boolean isHeadQuarter = Boolean.FALSE; // 是否总部

  private Boolean isCompany = Boolean.FALSE; // 是否公司

  private Boolean isDc = Boolean.FALSE; // 是否配送中心

  private Boolean isStore = Boolean.FALSE; // 是否门店

  private Boolean isVendor = Boolean.FALSE; // 是否供应商

  private Boolean isCustomer = Boolean.FALSE; // 是否客户

  // 4-4-4-4. 用法同category.id. 是upperDomain的冗余,
  private String levelId; // 层级码
  public static final int LENGTH_LEVELID = CDomain.LENGTH_LEVELID;

  // =upperDomain.levelId+4位序列号(VALUE_LEVELRULE)
  // domainMgr.save时根据upperDomain设置.
  private IDomain upperDomain = null;

  private Date storeInstallTime = null;

  // private PriceGroup priceGroup = null;//价格组
  // public static final String CAPTION_PRICEGROUP = "价格组";

  // private DeliveryGroup deliveryGroup = null;//配货组
  // public static final String CAPTION_DELIVERYGROUP = "配货组";

  private Integer deliveryOrder; // 配货次序

  /** 定货到货天数 */
  private Integer orderDays = new Integer(1);

  /** 定制品到货天数 */
  private Integer customizedOrderDays = new Integer(1);

  private String dealRange;

  private String productStyle;

  private String transporter;

  private String level;

  private IProfile profile3 = new Profile();

  private IProfile profile4 = new Profile();

  public String getDealRange() {
    return dealRange;
  }

  public void setDealRange(String dealRange) {
    this.dealRange = dealRange;
  }

  public String getLevel() {
    return level;
  }

  public void setLevel(String level) {
    this.level = level;
  }

  public String getProductStyle() {
    return productStyle;
  }

  public void setProductStyle(String productStyle) {
    this.productStyle = productStyle;
  }

  public IProfile getProfile3() {
    return profile3;
  }

  public void setProfile3(IProfile profile3) {
    this.profile3 = profile3;
  }

  public IProfile getProfile4() {
    return profile4;
  }

  public void setProfile4(IProfile profile4) {
    this.profile4 = profile4;
  }

  public String getTransporter() {
    return transporter;
  }

  public void setTransporter(String transporter) {
    this.transporter = transporter;
  }

  public String debugInfo() {
    return getCode();
  }

  // getter/setter

  public String getBankAccount() {
    return bankAccount;
  }

  public void setBankAccount(String bankAccount) throws StringTooLongException {
    if (bankAccount != null) {
      bankAccount = truncate(bankAccount, LENGTH_BANKACCOUNT, "bankAccount ", CAPTION_BANKACCOUNT);
    }
    this.bankAccount = bankAccount;
  }

  public String getCode() {
    return code;
  }

  public void setCode(String code) throws StringTooLongException {
    if (code != null) {
      code = code.trim().toUpperCase();
      code = truncate(code, CDomain.LENGTH_CODE, "code", CDomain.CAPTION_CODE);
    }
    this.code = code;
  }

  public String getInvoiceCode() {
    return invoiceCode;
  }

  public void setInvoiceCode(String invoiceCode) throws StringTooLongException {
    if (invoiceCode != null) {
      invoiceCode = truncate(invoiceCode, LENGTH_INVOICECODE, "invoiceCode ", CAPTION_INVOICECODE);
    }
    this.invoiceCode = invoiceCode;
  }

  public Boolean isCompany() {
    return isCompany;
  }

  public void setCompany(Boolean isCompany) {
    this.isCompany = isCompany;
  }

  public Boolean isCustomer() {
    return isCustomer;
  }

  public void setCustomer(Boolean isCustomer) {
    this.isCustomer = isCustomer;
  }

  public Boolean isDc() {
    return isDc;
  }

  public void setDc(Boolean isDc) {
    this.isDc = isDc;
  }

  public Boolean isHeadQuarter() {
    return isHeadQuarter;
  }

  public void setHeadQuarter(Boolean isHeadQuarter) {
    this.isHeadQuarter = isHeadQuarter;
  }

  public Boolean isStore() {
    return isStore;
  }

  public void setStore(Boolean isStore) {
    this.isStore = isStore;
  }

  public Boolean isVendor() {
    return isVendor;
  }

  public void setVendor(Boolean isVendor) {
    this.isVendor = isVendor;
  }

  public String getLevelId() {
    return levelId;
  }

  public void setLevelId(String levelId) throws StringTooLongException {
    if (levelId != null) {
      levelId = truncate(levelId, CDomain.LENGTH_LEVELID, "levelId ", CDomain.CAPTION_LEVELID);
    }
    this.levelId = levelId;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) throws StringTooLongException {
    if (name != null) {
      name = truncate(name, CDomain.LENGTH_NAME, "name", CDomain.CAPTION_NAME);
    }
    this.name = name;
  }

  public IProfile getProfile() {
    return profile;
  }

  public void setProfile(IProfile profile) {
    this.profile = profile;
  }

  public String getRemark() {
    return remark;
  }

  public void setRemark(String remark) throws StringTooLongException {
    if (remark != null) {
      remark = truncate(remark, CDomain.LENGTH_REMARK, "remark ", CDomain.CAPTION_REMARK);
    }
    this.remark = remark;
  }

  public IDomain getSettleOrganization() {
    return settleOrganization;
  }

  public void setSettleOrganization(IDomain settleOrganization) {
    this.settleOrganization = settleOrganization;
  }

  public String getTaxCode() {
    return taxCode;
  }

  public void setTaxCode(String taxCode) throws StringTooLongException {
    if (taxCode != null) {
      taxCode = truncate(taxCode, LENGTH_TAXCODE, "taxCode ", CAPTION_TAXCODE);
    }
    this.taxCode = taxCode;
  }

  public Boolean getIsCompany() {
    return isCompany;
  }

  public void setIsCompany(Boolean isCompany) {
    this.isCompany = isCompany;
  }

  public Boolean getIsCustomer() {
    return isCustomer;
  }

  public void setIsCustomer(Boolean isCustomer) {
    this.isCustomer = isCustomer;
  }

  public Boolean getIsDc() {
    return isDc;
  }

  public void setIsDc(Boolean isDc) {
    this.isDc = isDc;
  }

  public Boolean getIsHeadQuarter() {
    return isHeadQuarter;
  }

  public void setIsHeadQuarter(Boolean isHeadQuarter) {
    this.isHeadQuarter = isHeadQuarter;
  }

  public Boolean getIsStore() {
    return isStore;
  }

  public void setIsStore(Boolean isStore) {
    this.isStore = isStore;
  }

  public Boolean getIsVendor() {
    return isVendor;
  }

  public void setIsVendor(Boolean isVendor) {
    this.isVendor = isVendor;
  }

  public IDomain getUpperDomain() {
    return upperDomain;
  }

  public void setUpperDomain(IDomain upperDomain) {
    this.upperDomain = upperDomain;
  }

  public Date getStoreInstallTime() {
    return storeInstallTime;
  }

  public void setStoreInstallTime(Date storeInstallTime) {
    this.storeInstallTime = storeInstallTime;
  }

  public IDomain getDomain() {
    return domain;
  }

  public void setDomain(IDomain domain) {
    this.domain = domain;
  }

  public String getLastModifier() {
    return lastModifier;
  }

  public void setLastModifier(String lastModifier) {
    this.lastModifier = lastModifier;
  }

  public Integer getState() {
    return state;
  }

  public void setState(Integer state) {
    this.state = state;
  }

  public Integer getDeliveryOrder() {
    return deliveryOrder;
  }

  public void setDeliveryOrder(Integer deliveryOrder) {
    this.deliveryOrder = deliveryOrder;
  }

  public Integer getOrderDays() {
    return orderDays;
  }

  public void setOrderDays(Integer orderDays) {
    this.orderDays = orderDays;
  }

  public IProfile getProfile2() {
    return profile2;
  }

  public void setProfile2(IProfile profile2) {
    this.profile2 = profile2;
  }

  public Integer getCustomizedOrderDays() {
    return customizedOrderDays;
  }

  public void setCustomizedOrderDays(Integer customizedOrderDays) {
    this.customizedOrderDays = customizedOrderDays;
  }

  /**
   * 得到Domain的单位性质中文名称.方便页面编码
   * 
   * @author 王志国
   */
  public String getPropertyCaption() {
    if (getIsHeadQuarter().booleanValue())
      return "总部";
    if (getIsCompany().booleanValue())
      return "公司";
    if (getIsDc().booleanValue())
      return "配送中心";
    if (getIsStore().booleanValue())
      return "门店";
    if (getIsVendor().booleanValue())
      return "供应商";
    if (getIsCustomer().booleanValue())
      return "客户";

    return "&nbsp;";
  }

  /**
   * 得到得到上级单位名称,若不存在上级单位返回空字符串.方便页面编码
   * 
   * @author 王志国
   */
  public String getUpperDomainName() throws BusinessException {
    if (getUpperDomain() == null)
      return "";
    return getUpperDomain().getName();
  }

  public String getCodeName() {
    return getCode() + "[" + getName() + "]";
  }

  public String getNameCode() {
    return getName() + "[" + getCode() + "]";
  }

  /**
   * @return 是否内部单位
   */
  public boolean isInternal() {
    return isHeadQuarter() != null && isHeadQuarter().booleanValue() || isDc() != null
        && isDc().booleanValue() || isCompany() != null && isCompany().booleanValue()
        || isStore() != null && isStore.booleanValue();
  }

  private Map cfvMap = null;

  public boolean hasCFV(String cfvUuid) throws BusinessException, HibernateException {
    buildCFVMap();
    return cfvMap.containsKey(cfvUuid);
  }

  private void buildCFVMap() throws BusinessException, HibernateException {
    if (cfvMap != null)
      return;
    IDomainMgr dmMgr = (IDomainMgr) NamingMgr.getInstance().lookupMgr(IDomainMgr.class);
    cfvMap = new HashMap();
    List<ICartFunctionView> cfvs = dmMgr.getCFVsByDomain(this);
    for (int i = 0; i < cfvs.size(); i++)
      cfvMap.put(cfvs.get(i).getUuid(), cfvs.get(i).getUuid());
  }

}
